<?php

namespace App\Http\Controllers\Backstage\Article;

use App\Models\ArticleComment;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;

//文章评论管理

class ArticleCommentController extends Controller
{
    public function getCommentList(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        //翻页
        $pagenNum = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        if ($pageSize > 50) {
            return false;
        }
        $page = $pagenNum - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
        }
        //联表查询
        $count = ArticleComment::count();
        $info = ArticleComment::orderByDesc("comment_create_time")
            ->leftJoin('user', 'article_comment.user_id', '=', 'user.user_id')
            ->leftJoin('article', 'article_comment.aid', '=', 'article.aid')
            ->select('article_comment.comment_id', 'article_comment.comment_content', 'article_comment.comment_create_time', 'article_comment.isshow', 'article.article_title', 'user.nickName', 'user.avatarUrl')
            ->offset($page)
            ->limit($pageSize)
            ->get();
        if ($info) {
            $info = json_decode($info, true);
            foreach ($info as $key => $value) {
                $info[$key]['nickName']=urldecode($value['nickName']);
                $info[$key]['comment_create_time'] = date("Y-m-d", $value['comment_create_time']);
                $value['isshow'] ? $info[$key]['isshow'] = true : $info[$key]['isshow'] = false;
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        $data['count'] = $count;
        return $data;
    }
    //设置审核
    public function setCommentStatus(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['comment_id'] = (int) $request->query('comment_id');
        $info = ArticleComment::where($map)->first();
        $info->isshow ? $date['isshow'] = 0 : $date['isshow'] = 1;
        $msg = ArticleComment::where($map)->update($date);
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
    //删除文章评论
    public function delArticleComment(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['comment_id'] = (int) $request->query('comment_id');
        $msg = ArticleComment::where($map)->delete();
        if ($msg) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }
}
